package edu.ncsu.csc.ip.server.timer;

import java.util.Map;
import java.util.TimerTask;
import edu.ncsu.csc.ip.server.ClientBean;
import edu.ncsu.csc.ip.server.ClientIndex;

/**
 * Class <code></code>
 * 
 * 
 * 
 * @author Moin Ayazifar <mayazif@ncsu.edu>
 * @author Steven Elliott <sdelliot@ncsu.edu>
 * 
 */

public class TTLUpdateTask extends TimerTask {

	@Override
	public void run() {
		Map<Integer, ClientBean> cllientIndexMap = ClientIndex.getInstance()
				.getclientIndexMap();
		ClientBean clientBean;
		for (Integer cookie : cllientIndexMap.keySet()) {
			clientBean = cllientIndexMap.get(cookie);
			int ttl;
			if (clientBean.getTtl() != null
					&& (ttl = clientBean.getTtl()) > ClientIndex.TTL_UPDATE_INTERVAL) {

				clientBean.setTtl(ttl - ClientIndex.TTL_UPDATE_INTERVAL);
			} else {
				clientBean.setTtl(0);
				clientBean.setActiveFlag(false);
				clientBean.setActivityCount(0);
			}
		}

	}
}
